#include "MT6701_Driver.h"

#include "SPI_Driver.h"

#pragma region //==========字段=========
static sSPI_DRIVER *pCodeDriver; //指向驱动对象
//CRC_POLY = 0x3 X6+X1+X0 = x6+x+1
//CRC_BIT = 6
static const unsigned char CRC_3[256] = {
  0x00, 0x0C, 0x18, 0x14, 0x30, 0x3C, 0x28, 0x24, 0x60, 0x6C, 0x78, 0x74, 0x50, 0x5C, 0x48, 0x44,
  0xC0, 0xCC, 0xD8, 0xD4, 0xF0, 0xFC, 0xE8, 0xE4, 0xA0, 0xAC, 0xB8, 0xB4, 0x90, 0x9C, 0x88, 0x84,
  0x8C, 0x80, 0x94, 0x98, 0xBC, 0xB0, 0xA4, 0xA8, 0xEC, 0xE0, 0xF4, 0xF8, 0xDC, 0xD0, 0xC4, 0xC8,
  0x4C, 0x40, 0x54, 0x58, 0x7C, 0x70, 0x64, 0x68, 0x2C, 0x20, 0x34, 0x38, 0x1C, 0x10, 0x04, 0x08,
  0x14, 0x18, 0x0C, 0x00, 0x24, 0x28, 0x3C, 0x30, 0x74, 0x78, 0x6C, 0x60, 0x44, 0x48, 0x5C, 0x50,
  0xD4, 0xD8, 0xCC, 0xC0, 0xE4, 0xE8, 0xFC, 0xF0, 0xB4, 0xB8, 0xAC, 0xA0, 0x84, 0x88, 0x9C, 0x90,
  0x98, 0x94, 0x80, 0x8C, 0xA8, 0xA4, 0xB0, 0xBC, 0xF8, 0xF4, 0xE0, 0xEC, 0xC8, 0xC4, 0xD0, 0xDC,
  0x58, 0x54, 0x40, 0x4C, 0x68, 0x64, 0x70, 0x7C, 0x38, 0x34, 0x20, 0x2C, 0x08, 0x04, 0x10, 0x1C,
  0x28, 0x24, 0x30, 0x3C, 0x18, 0x14, 0x00, 0x0C, 0x48, 0x44, 0x50, 0x5C, 0x78, 0x74, 0x60, 0x6C,
  0xE8, 0xE4, 0xF0, 0xFC, 0xD8, 0xD4, 0xC0, 0xCC, 0x88, 0x84, 0x90, 0x9C, 0xB8, 0xB4, 0xA0, 0xAC,
  0xA4, 0xA8, 0xBC, 0xB0, 0x94, 0x98, 0x8C, 0x80, 0xC4, 0xC8, 0xDC, 0xD0, 0xF4, 0xF8, 0xEC, 0xE0,
  0x64, 0x68, 0x7C, 0x70, 0x54, 0x58, 0x4C, 0x40, 0x04, 0x08, 0x1C, 0x10, 0x34, 0x38, 0x2C, 0x20,
  0x3C, 0x30, 0x24, 0x28, 0x0C, 0x00, 0x14, 0x18, 0x5C, 0x50, 0x44, 0x48, 0x6C, 0x60, 0x74, 0x78,
  0xFC, 0xF0, 0xE4, 0xE8, 0xCC, 0xC0, 0xD4, 0xD8, 0x9C, 0x90, 0x84, 0x88, 0xAC, 0xA0, 0xB4, 0xB8,
  0xB0, 0xBC, 0xA8, 0xA4, 0x80, 0x8C, 0x98, 0x94, 0xD0, 0xDC, 0xC8, 0xC4, 0xE0, 0xEC, 0xF8, 0xF4,
  0x70, 0x7C, 0x68, 0x64, 0x40, 0x4C, 0x58, 0x54, 0x10, 0x1C, 0x08, 0x04, 0x20, 0x2C, 0x38, 0x34
};
//编码器SPI通信数据结构
union uCoderData
{
	uint8_t byte[4];
	uint32_t data;
} raw_data;

#pragma endregion

#pragma region //===========方法===========
/*
********************************************************************************
*描    述 : 注册SPI驱动变量并初始化MT6701
*返    回 : 0:成功；1--重复初始化已经注册过了；-1--spi_drive变量已经用完，需调整USE_SPI_NUM数据。
********************************************************************************/
int MT6701_Init(SPI_HandleTypeDef *hspi, osSemaphoreId done_sem)
{
	int ret;
	ret = SPI_Init(&pCodeDriver, hspi, done_sem);
	
//	HAL_GPIO_WritePin(CODER_CS_GPIO_Port, CODER_CS_Pin, GPIO_PIN_SET);
	return ret;
}
static uint8_t check_crc(uint8_t *pt, int len)
{
	uint8_t crc = 0;//初始值为0
	for (size_t i = 0; i < len; i++)
	{
		crc ^= *pt++;
		crc = CRC_3[crc];
	}
	return crc;
}
/*
********************************************************************************
*描    述 : 从MT6701中获得原始角度数据0~8192(2^13),14位始终为1(有数据),波特率<=164Kbit/s!!!
*返    回 : statues=0或4:返回角度数据；statues=其它:返回值无效。
********************************************************************************/
int MT6701_GetRawData(CoderData_t *coder_data)
{
//	HAL_GPIO_WritePin(CODER_CS_GPIO_Port, CODER_CS_Pin, GPIO_PIN_RESET);
	raw_data.data = 0; //清零
	SPI_Only_Rx(pCodeDriver, (uint8_t*)&raw_data.byte[1], 3);
//	HAL_GPIO_WritePin(CODER_CS_GPIO_Port, CODER_CS_Pin, GPIO_PIN_SET);
	coder_data->data = __REV(raw_data.data);
		
	//crc check
	return check_crc(&raw_data.byte[1], 3);
}
#pragma endregion
